/*
 * maxSpeedSetWin.c
 *
 *  Created on: 2021年11月1日
 *      Author: 王慧云
 */


#include "maxSpeedSetWin.h"

/*创建一个键盘事件组*/
static lv_group_t* keyGroup;

/*创建一个输入框*/
static lv_obj_t * spinbox;

/**退出本界面回调函数**/
static void exit_screen(void* scr)
{
	lv_obj_del(scr);
}

/**切换数字输入框的编辑状态 回调函数**/
static void spinbox_cb(lv_event_t * event)
{
    /**判断是否是编辑状态**/
	if(lv_group_get_editing(keyGroup))
	{
		//printf("LV_STATE_FOCUSED\r\n");
		lv_group_set_editing(keyGroup, false);
	}
	else
	{
		//printf("!LV_STATE_FOCUSED\r\n");
		lv_group_set_editing(keyGroup, true);
		//printf("set LV_STATE_FOCUSED\r\n");
	}
}


/**"Enter"按键回调函数**/
static void enter_btn_cb(lv_event_t * e)
{
	//保存并退出
	motor_set_maxSpeed(lv_spinbox_get_value(spinbox));
	lv_group_del(keyGroup);
	lv_group_set_default(NULL);
	lv_async_call(exit_screen, lv_scr_act());
	menu(1);
}

/**"Cancel"按钮回调函数**/
static void cancel_btn_cb(lv_event_t * e)
{
	lv_group_del(keyGroup); //不保存退出
	lv_group_set_default(NULL);
	lv_async_call(exit_screen, lv_scr_act());
	menu(1);
}



void open_maxSpeedSetWin()
{
	lv_obj_t*  scr = lv_obj_create(NULL);
	lv_scr_load(scr);

	spinbox = lv_spinbox_create(lv_scr_act());
	lv_spinbox_set_range(spinbox, 10, 65535);
	lv_spinbox_set_digit_format(spinbox, 5, 0);
	lv_spinbox_set_step(spinbox,1);
	lv_obj_set_width(spinbox, 60);
	lv_spinbox_set_value(spinbox, motor_get_maxSpeed());
	lv_obj_center(spinbox);
	lv_obj_align(spinbox,LV_ALIGN_LEFT_MID,20,0);
	lv_obj_add_event_cb(spinbox, spinbox_cb, LV_EVENT_PRESSED, NULL);
	
	lv_obj_t* label_title = lv_label_create(lv_scr_act()); //标题标签
	lv_obj_set_width(label_title, 160);
	lv_obj_set_style_text_font(label_title, &yahei_16, 0);
	lv_obj_add_style(label_title, &style_label_3, LV_PART_MAIN);
	lv_obj_align(label_title, LV_ALIGN_TOP_MID, 0, 0);
	lv_label_set_text(label_title, "最大转速设置");
	
	lv_obj_t* label = lv_label_create(lv_scr_act()); 
	lv_label_set_text(label, "rpm");
	lv_obj_align(label, LV_ALIGN_RIGHT_MID, -40, 0);
	
	lv_obj_t* btn1 = lv_btn_create(lv_scr_act());
	label = lv_label_create(btn1);
	lv_label_set_text(label, "Enter");
	lv_obj_align_to(label, btn1, LV_ALIGN_CENTER, 0, -5);
	lv_obj_set_size(btn1, LV_SIZE_CONTENT, 16);
	lv_obj_align(btn1, LV_ALIGN_BOTTOM_MID, -40, -3);
	lv_obj_add_event_cb(btn1, enter_btn_cb, LV_EVENT_PRESSED, NULL);

	lv_obj_t* btn2 = lv_btn_create(lv_scr_act());
	label = lv_label_create(btn2);
	lv_label_set_text(label, "Cancel");
	lv_obj_align_to(label, btn2, LV_ALIGN_CENTER, 0, -5);
	lv_obj_set_size(btn2, LV_SIZE_CONTENT, 16);
	lv_obj_align(btn2, LV_ALIGN_BOTTOM_MID, 40, -3);
	lv_obj_add_event_cb(btn2, cancel_btn_cb, LV_EVENT_PRESSED, NULL);

	keyGroup = lv_group_create();
	lv_indev_set_group(indev_keypad, keyGroup);
	lv_group_set_default(keyGroup);

	lv_group_add_obj(keyGroup, spinbox);
	lv_group_add_obj(keyGroup, btn1);
	lv_group_add_obj(keyGroup, btn2);
}
